package com.my.code.threadpool;

import java.util.concurrent.BlockingQueue;

/**
 * 工作线程
 *
 * @author zzl
 * @date 2020/10/14
 */
public class WorkThread extends Thread {

    private volatile boolean on = true;

    private final BlockingQueue<Runnable> taskQueue;

    public WorkThread(BlockingQueue<Runnable> taskQueue) {
        this.taskQueue = taskQueue;
    }

    @Override
    public void run() {
        while (on && !isInterrupted()) {
            try {
                Runnable task = taskQueue.take();
                task.run();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void cancel() {
        on = false;
        interrupt();
    }
}
